package com.yanqu.road.server.manager.activity.hsisland.pb;

import com.yanqu.road.entity.activity.hsisland.*;
import com.yanqu.road.entity.activity.hsisland.data.*;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandBattleFieldEnemyEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandEventIdEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandFinishFlagEnum;
import com.yanqu.road.entity.activity.hsisland.enums.HsIslandYnFlagEnum;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.manager.activity.hsisland.*;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.activity.hsisland.rank.CrossHsIslandRankListModel;
import com.yanqu.road.server.manager.activity.hsisland.rank.HsIslandRank;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossHsIslandActivityPb {

    /**
     * build 玩家数据
     * @param user
     * @return
     */
    public static HsIslandProto.HsIslandUserData.Builder buildHsIslandUserData(CrossHsIslandUser user) {
        HsIslandProto.HsIslandUserData.Builder builder = HsIslandProto.HsIslandUserData.newBuilder();
        builder.setUserId(user.getUserData().getUserId());
        builder.setUnionId(user.getUserData().getUnionId());
        builder.setEnergy(user.getUserData().getEnergy());
        builder.setEnergyLastTime(user.getUserData().getEnergyRestoreTime());
        builder.setRank(0);
        builder.setScore(user.getUserData().getScore());
        builder.setPower(user.getPower());
        builder.setHelpNum(user.getUserData().getHelpNum());
        builder.setImageId(user.getUserData().getImageId());
        builder.setFloorId(user.getUserData().getFloorId());
        builder.setIsLand(user.getUserData().getIslandId());
        builder.setPosition(buildHsIslandUserPositionData(user.getUserData().getX(), user.getUserData().getY()));
        builder.setCanPveInviteNum(user.getUserData().getCanPveInviteNum());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(user.getUserData().getUserId());
        if (userBaseInfo != null) {
            builder.setUserBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        //战力加成
        builder.setPowerAddRate(user.getPowerAddRate());
        //解锁的形象
        builder.addAllUnlockImageIds(user.getUserData().getUnlockedImageIds());
        //获取商会
        CrossHsIslandUnion union = CrossHsIslandActivityMgr.getUnion(user.getUserData().getActivityId(), user.getUserData().getGroupId(), user.getUserData().getUnionId());
        if (union != null) {
            builder.setUnionData(buildHsIslandUnionData(union));
        }
        //门客数据
        for (HsIslandPatronsData patron : user.getUserData().getPatronsMap().values()) {
            builder.addPatrons(buildHsIslandPatronsData(patron, user.getPowerAddRate()));
        }
        //BUFF信息
        for (HsIslandBuffData buff : user.getUserData().getBuffMap().values()) {
            builder.addBuffs(buildHsIslandBuffData(buff));
        }
        builder.setActiveTime(user.getUserData().getActiveTime());
        builder.addAllHadRewardBattleFieldIdList(user.getUserData().getHadRewardBattleFieldIdList());
        builder.setDonateNum(user.getUserData().getDonateNum());
        builder.setDUsableDonateNum(user.getUserData().getDUsableDonateNum());
        return builder;
    }

    /**
     * build 红点数据
     * @param helpRewardRedDot
     * @param bossRewardRedDot
     * @param boxRewardRedDot
     * @param newFloorRedDot
     * @return
     */
    public static HsIslandProto.HsIslandSyncRedDotMsg.Builder buildHsIslandSyncRedDotMsg(boolean helpRewardRedDot, boolean bossRewardRedDot, boolean boxRewardRedDot, boolean newFloorRedDot) {
        HsIslandProto.HsIslandSyncRedDotMsg.Builder builder = HsIslandProto.HsIslandSyncRedDotMsg.newBuilder();
        builder.setHelpReward(helpRewardRedDot);
        builder.setBossReward(bossRewardRedDot);
        builder.setBoxReward(boxRewardRedDot);
        builder.setNewFloor(newFloorRedDot);
        return builder;
    }

    /**
     * build 商会
     * @param union
     * @return
     */
    public static HsIslandProto.HsIslandUnionData.Builder buildHsIslandUnionData(CrossHsIslandUnion union) {
        HsIslandProto.HsIslandUnionData.Builder builder = HsIslandProto.HsIslandUnionData.newBuilder();
        builder.setUnionId(union.getUnionData().getUnionId());
        builder.setBoatLevel(union.getUnionData().getLevel());
        builder.setBoatExp(union.getUnionData().getExp());
        builder.setDoubleAllDonateNum(union.getUnionData().getDAllDonateNum());
        builder.setDoubleUsedDonateNum(union.getUnionData().getDAllDonateNum() - union.getUnionData().getDUsableDonateNum());
        builder.setMaxFloorId(union.getUnionData().getMaxFloorId());
        builder.setMaxFloorExploreProgress(union.getMaxFloorExploreProgress());
        return builder;
    }

    /**
     * build 坐标
     * @param x
     * @param y
     * @return
     */
    public static HsIslandProto.HsIslandUserPositionData.Builder buildHsIslandUserPositionData(int x, int y) {
        HsIslandProto.HsIslandUserPositionData.Builder builder = HsIslandProto.HsIslandUserPositionData.newBuilder();
        builder.setX(x);
        builder.setY(y);
        return builder;
    }

    /**
     * build 海域数据
     * @param floor
     * @return
     */
    public static HsIslandProto.HsIslandFloorData.Builder buildHsIslandFloorData(CrossHsIslandFloor floor) {
        HsIslandProto.HsIslandFloorData.Builder builder = HsIslandProto.HsIslandFloorData.newBuilder();
        builder.setFloorId(floor.getFloorData().getFloorId());
        //岛数据
        for (CrossHsIslandIsland island : floor.getIslandMap().values()) {
            HsIslandProto.HsIslandIsLandData.Builder isLandBuilder = buildHsIslandIsLandData(floor, island);
            builder.addIslands(isLandBuilder);
        }
        //玩家数据
        for (CrossHsIslandUser user : floor.getUserMap().values()) {
            HsIslandProto.HsIslandUserData.Builder userBuilder = buildHsIslandUserData(user);
            builder.addUsers(userBuilder);
        }
        return builder;
    }

    /**
     * build 岛数据
     * @param island
     * @return
     */
    public static HsIslandProto.HsIslandIsLandData.Builder buildHsIslandIsLandData(CrossHsIslandFloor floor, CrossHsIslandIsland island) {
        HsIslandProto.HsIslandIsLandData.Builder builder = HsIslandProto.HsIslandIsLandData.newBuilder();
        builder.setIsLandId(island.getIslandData().getIslandId());
        builder.setKeyNum(island.getIslandData().getKeyNum());
        builder.setExploreProgress(island.getExploreProgress());
        //未完结事件MAP
        for (HsIslandEventData eventData : island.getEventMap().values()) {
            for (HsIslandPosition position : eventData.getPositions()) {
                if (eventData.getFinishFlag() == HsIslandFinishFlagEnum.FINISH_NOT.getType()) {
                    HsIslandProto.HsIslandLatticeData.Builder latticeBuilder = buildHsIslandLatticeData(position, eventData.getFinishFlag(), eventData);
                    builder.addLattice(latticeBuilder);
                }
            }
        }
        //已完结事件MAP
        for (String positionStr: island.getFinishEventSet()) {
            HsIslandPosition position = new HsIslandPosition(positionStr);
            //宝箱比较特殊，已完结也要展示
            if (floor.getFloorData().getBoxIslandId() == island.getIslandData().getIslandId() && floor.getBox(positionStr) != null) {
                HsIslandBoxData box = floor.getBox(positionStr);
                HsIslandProto.HsIslandLatticeData.Builder latticeBuilder = buildHsIslandBoxLatticeData(position, floor, box);
                builder.addLattice(latticeBuilder);
            } else {
                HsIslandProto.HsIslandLatticeData.Builder latticeBuilder = buildHsIslandLatticeData(position, HsIslandFinishFlagEnum.FINISH_YES.getType(), null);
                builder.addLattice(latticeBuilder);
            }
        }
        return builder;
    }

    /**
     * builder 岛数据（简易版）
     * @param island
     * @return
     */
    public static HsIslandProto.HsIslandIsLandData.Builder buildHsIslandIsLandDataSimple(CrossHsIslandIsland island) {
        HsIslandProto.HsIslandIsLandData.Builder builder = HsIslandProto.HsIslandIsLandData.newBuilder();
        builder.setIsLandId(island.getIslandData().getIslandId());
        builder.setKeyNum(island.getIslandData().getKeyNum());
        builder.setExploreProgress(island.getExploreProgress());
        return builder;
    }

    /**
     * build 格子数据
     * @param position
     * @param finishFlag
     * @param eventData
     * @return
     */
    public static HsIslandProto.HsIslandLatticeData.Builder buildHsIslandLatticeData(HsIslandPosition position, int finishFlag, HsIslandEventData eventData) {
        HsIslandProto.HsIslandLatticeData.Builder builder = HsIslandProto.HsIslandLatticeData.newBuilder();
        builder.setPosition(buildHsIslandUserPositionData(position.getX(), position.getY()));
        builder.setFinishFlag(finishFlag);
        if (eventData != null) {
            builder.setEvent(buildHsIslandEventData(eventData, false));
        }
        return builder;
    }

    /**
     * build 宝箱格子数据
     * @param position
     * @param floor
     * @param box
     * @return
     */
    public static HsIslandProto.HsIslandLatticeData.Builder buildHsIslandBoxLatticeData(HsIslandPosition position, CrossHsIslandFloor floor, HsIslandBoxData box) {
        HsIslandProto.HsIslandLatticeData.Builder builder = HsIslandProto.HsIslandLatticeData.newBuilder();
        builder.setPosition(buildHsIslandUserPositionData(position.getX(), position.getY()));
        builder.setFinishFlag(box.getUserId() > 0 ? HsIslandFinishFlagEnum.FINISH_YES.getType() : HsIslandFinishFlagEnum.FINISH_NOT.getType());
        builder.setEvent(buildHsIslandBoxEventData(floor, box));
        return builder;
    }

    /**
     * build 宝箱数据
     * @param box
     * @return
     */
    public static HsIslandProto.HsIslandBoxData.Builder buildHsIslandBoxData(HsIslandBoxData box) {
        HsIslandProto.HsIslandBoxData.Builder builder = HsIslandProto.HsIslandBoxData.newBuilder();
        builder.setBoxId(box.getBoxId());
        builder.setPosition(buildHsIslandUserPositionData(box.getX(), box.getY()));
        builder.setUserId(box.getUserId());
        builder.setReward(box.getReward());
        if (box.getUserId() > 0) {
            //获取玩家名称
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(box.getUserId());
            if (userBaseInfo != null) {
                builder.setUserName(userBaseInfo.getNickName());
            }
        }
        return builder;
    }

    /**
     * build buff
     * @param buffData
     * @return
     */
    public static HsIslandProto.HsIslandBuffData.Builder buildHsIslandBuffData(HsIslandBuffData buffData) {
        HsIslandProto.HsIslandBuffData.Builder builder = HsIslandProto.HsIslandBuffData.newBuilder();
        builder.setBuffId(buffData.getBuffId());
        builder.setTimes(buffData.getTimes());
        return builder;
    }

    /**
     * build 怪物数据
     *
     * @param activityId
     * @param groupId
     * @param monsterData
     * @return
     */
    public static HsIslandProto.HsIslandMonsterData.Builder buildHsIslandMonsterData(int activityId, int groupId, HsIslandMonsterData monsterData, boolean showReBlood) {
        HsIslandProto.HsIslandMonsterData.Builder builder = HsIslandProto.HsIslandMonsterData.newBuilder();
        builder.setMonsterId(monsterData.getMonsterId());
        builder.setAllBlood(monsterData.getAllBlood());
        builder.setReBlood(monsterData.getReBlood());
        builder.setCreateTime(monsterData.getCreateTime());
        builder.setFindUserId(monsterData.getFindUserId());

        if (showReBlood) {
            CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(activityId, groupId);
            CrossHsIslandBattleFieldDamage battleFieldDamage = activity.getBattleFieldDamage(monsterData.getBattleFieldId());
            if (battleFieldDamage != null) {
                for (HsIslandBattleFieldDamageData damageData : battleFieldDamage.getDamageDataMap().values()) {
                    builder.addReBloodData(buildHsIslandReBloodData(damageData));
                }
            }
        }

        return builder;
    }

    /**
     * build 掉血数据
     */
    public static HsIslandProto.HsIslandReBloodData.Builder buildHsIslandReBloodData(HsIslandBattleFieldDamageData damageData) {
        HsIslandProto.HsIslandReBloodData.Builder builder = HsIslandProto.HsIslandReBloodData.newBuilder();
        builder.setUserId(damageData.getUserId());
        builder.setReBlood(damageData.getTotalDamage());
        builder.setTimes(damageData.getTimes());
        builder.setTime(damageData.getUpdateTime());
        //获取玩家名称
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(damageData.getUserId());
        if (userBaseInfo != null) {
            builder.setUserName(userBaseInfo.getNickName());
        }
        builder.setUnionId(damageData.getUnionId());
        return builder;
    }

    /**
     * build pvp战斗数据
     * @param eventData
     * @return
     */
    public static HsIslandProto.HsIslandUnionBattleData.Builder buildHsIslandUnionBattleData(HsIslandEventData eventData) {
        HsIslandProto.HsIslandUnionBattleData.Builder builder = HsIslandProto.HsIslandUnionBattleData.newBuilder();
        builder.setUnionId(eventData.getPvpUnionId());
        UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(eventData.getActivityId(), eventData.getPvpUnionId(), eventData.getGroupId());
        if (groupUnion != null) {
            builder.setUnionData(UnionBasePb.parseUnionBaseTempMsg(groupUnion.getUnionBaseInfo()));
        }
        for (long userId : eventData.getPvpUserIds()) {
            CrossHsIslandUser pvpUser = CrossHsIslandActivityMgr.getUser(eventData.getActivityId(), eventData.getGroupId(), userId);
            if (pvpUser != null) {
                builder.addTargetUserData(buildHsIslandUserData(pvpUser));
            }
        }
        return builder;
    }

    /**
     * build 领取的遗物奖励数据
     * @param rewardData
     * @return
     */
    public static HsIslandProto.HsIslandEventRelicRewardData.Builder buildHsIslandEventRelicRewardData(HsIslandReceiveRelicRewardData rewardData) {
        HsIslandProto.HsIslandEventRelicRewardData.Builder builder = HsIslandProto.HsIslandEventRelicRewardData.newBuilder();
        builder.setIndex(rewardData.getIndex());
        builder.setReward(rewardData.getReward());
        builder.setRelicConfigId(rewardData.getRelicConfigId());
        builder.setReceiveFlag(rewardData.getReceiveFlag());
        return builder;
    }

    /**
     * build 宝箱事件事件数据
     * @param floor
     * @param box
     * @return
     */
    public static HsIslandProto.HsIslandEventData.Builder buildHsIslandBoxEventData(CrossHsIslandFloor floor, HsIslandBoxData box) {
        HsIslandProto.HsIslandEventData.Builder builder = HsIslandProto.HsIslandEventData.newBuilder();
        builder.setEventUnId(0);
        builder.setFloorId(floor.getFloorData().getFloorId());
        builder.setIsLandId(floor.getFloorData().getBoxIslandId());
        builder.addPosition(buildHsIslandUserPositionData(box.getX(), box.getY()));
        builder.setEventId(HsIslandEventIdEnum.EVENT_BOX.getType());
        builder.setUserId(floor.getFloorData().getBoxFindUserId());
        builder.setFinishFlag(box.getUserId() > 0 ? HsIslandYnFlagEnum.YES.getType() : HsIslandYnFlagEnum.NOT.getType());
        builder.setRewardFlag(box.getUserId() > 0 ? HsIslandYnFlagEnum.YES.getType() : HsIslandYnFlagEnum.NOT.getType());
        builder.setBox(buildHsIslandBoxData(box));
        return builder;
    }

    /**
     * build 事件数据
     * @param eventData
     * @param showUserBase
     * @return
     */
    public static HsIslandProto.HsIslandEventData.Builder buildHsIslandEventData(HsIslandEventData eventData, boolean showUserBase) {
        HsIslandProto.HsIslandEventData.Builder builder = HsIslandProto.HsIslandEventData.newBuilder();
        builder.setEventUnId(eventData.getEventUnId());
        builder.setFloorId(eventData.getFloorId());
        builder.setIsLandId(eventData.getIslandId());
        for (HsIslandPosition position : eventData.getPositions()) {
            builder.addPosition(buildHsIslandUserPositionData(position.getX(), position.getY()));
        }
        builder.setEventId(eventData.getEventId());
        builder.setUserId(eventData.getUserId());
        builder.setFinishFlag(eventData.getFinishFlag());
        builder.setRewardFlag(eventData.getRewardFlag());
        //以下为不同事件的特有数据
        //PVE数据
        if (eventData.getMonsterData() != null) {
            builder.setMonster(buildHsIslandMonsterData(eventData.getActivityId(), eventData.getGroupId(), eventData.getMonsterData(), false));
        }
        //PVP数据
        if (!StringUtils.isNullOrEmpty(eventData.getPvpUnionId()) && !eventData.getPvpUserIds().isEmpty()) {
            builder.setUnionBattle(buildHsIslandUnionBattleData(eventData));
        }
        //buff数据
        builder.setBuffId(eventData.getBuffId());
        //遗迹藏宝数据
        if (eventData.getReceiveRelicRewardMap() != null) {
            for (HsIslandReceiveRelicRewardData rewardData :eventData.getReceiveRelicRewardMap().values()) {
                builder.addReceivedRelicReward(buildHsIslandEventRelicRewardData(rewardData));
            }
        }
        //日志数据
        if (showUserBase) {
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(eventData.getUserId());
            if (userBaseInfo != null) {
                builder.setFindUser(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
            if (eventData.getMonsterWinUserId() > 0) {
                UserBaseInfo monsterWinUserBaseInfo = CrossUserMgr.getUserBaseInfo(eventData.getMonsterWinUserId());
                if (monsterWinUserBaseInfo != null) {
                    builder.setWinUser(PlayerBasePb.parsePlayerBaseTempMsg(monsterWinUserBaseInfo));
                }
            }
        }
        //2选1数据
        builder.setChooseConfigId(eventData.getChooseConfigId());
        //宝箱ID
        if (eventData.getBoxId() > 0) {
            //从海域上获取宝箱信息
            CrossHsIslandFloor floor = CrossHsIslandActivityMgr.getFloor(eventData.getActivityId(), eventData.getGroupId(), eventData.getUnionId(), eventData.getFloorId());
            if (floor != null) {
                for (HsIslandPosition boxPosition : eventData.getPositions()) {
                    HsIslandBoxData boxData = floor.getBox(boxPosition.getX(), boxPosition.getY());
                    if (boxData != null) {
                        builder.setBox(buildHsIslandBoxData(boxData));
                    }
                }
            }
        }
        //圣柱奖励ID
        builder.setGiftId(eventData.getGiftConfigId());
        //事件上的buff
        for (int buffId : eventData.getBuffIds()) {
            builder.addEffectBuffIds(buffId);
        }
        //事件时间
        builder.setCreateTime(eventData.getCreateTime());
        return builder;
    }

    /**
     * build 奖励数据
     * @param rewardData
     * @return
     */
    public static HsIslandProto.HsIslandRewardData.Builder buildHsIslandRewardData(HsIslandReward rewardData) {
        HsIslandProto.HsIslandRewardData.Builder builder = HsIslandProto.HsIslandRewardData.newBuilder();
        builder.setScore(rewardData.getScore());
        builder.setReward(PropertyHelper.parsePropertyToString(rewardData.getReward()));
        builder.setMoreWinReward(PropertyHelper.parsePropertyToString(rewardData.getMoreWinReward()));
        builder.setScoreMul(rewardData.getScoreAddRate());
        builder.setRewardMul(rewardData.getRewardAddRate());
        return builder;
    }

    /**
     * BUILD PVP战斗结果
     * @param result
     * @return
     */
    public static HsIslandProto.HsIslandPvpBattleResultData.Builder buildHsIslandPvpBattleResultData(HsIslandPvpBattleResult result) {
        HsIslandProto.HsIslandPvpBattleResultData.Builder builder = HsIslandProto.HsIslandPvpBattleResultData.newBuilder();
        builder.setWinFlag(result.isWinFlag());
        builder.setWinNum(result.getWinNum());
        builder.setEventData(buildHsIslandEventData(result.getEventData(), false));
        builder.setRewardData(buildHsIslandRewardData(result.getHsIslandReward()));
        return builder;
    }

    /**
     * build 探索结果
     * @param result
     * @return
     */
    public static HsIslandProto.HsIslandExploreResult.Builder buildHsIslandExploreResult(HsIslandExploreResult result) {
        HsIslandProto.HsIslandExploreResult.Builder builder = HsIslandProto.HsIslandExploreResult.newBuilder();
        for (HsIslandEventData eventData : result.getEventDataList()) {
            for (HsIslandPosition position : eventData.getPositions()) {
                HsIslandProto.HsIslandLatticeData.Builder latticeData = CrossHsIslandActivityPb.buildHsIslandLatticeData(position, eventData.getFinishFlag(), eventData);
                builder.addLattices(latticeData);
            }
        }
        if (result.getReward() != null) {
            builder.setRewardData(buildHsIslandRewardData(result.getReward()));
        }
        return builder;
    }

    /**
     * build floorBoxData
     * @param floorBoxData
     * @return
     */
    public static HsIslandProto.HsIslandFloorBoxData.Builder buildHsIslandFloorBoxData(HsIslandFloorBoxData floorBoxData) {
        HsIslandProto.HsIslandFloorBoxData.Builder builder = HsIslandProto.HsIslandFloorBoxData.newBuilder();
        builder.setFloorId(floorBoxData.getFloorId());
        builder.setAllNum(floorBoxData.getAllNum());
        builder.setResidueNum(floorBoxData.getResidueNum());
        builder.setMyAllNum(floorBoxData.getMyAllNum());
        builder.setMyReceivedNum(floorBoxData.getMyReceivedNum());
        for (HsIslandFloorBoxReceive item : floorBoxData.getReceiveList()) {
            builder.addReceiveData(buildHsIslandFloorBoxReceiveData(item));
        }
        return builder;
    }

    /**
     * build floorBoxReceive
     * @param floorBoxReceive
     * @return
     */
    public static HsIslandProto.HsIslandFloorBoxReceiveData.Builder buildHsIslandFloorBoxReceiveData(HsIslandFloorBoxReceive floorBoxReceive) {
        HsIslandProto.HsIslandFloorBoxReceiveData.Builder builder = HsIslandProto.HsIslandFloorBoxReceiveData.newBuilder();
        builder.setUserId(floorBoxReceive.getUserId());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(floorBoxReceive.getUserId());
        if (userBaseInfo != null) {
            builder.setUserBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        builder.setReward(floorBoxReceive.getReward());
        return builder;
    }

    /**
     * build 商会日志
     * @param logData
     * @return
     */
    public static HsIslandProto.HsIslandUnionLogData.Builder buildHsIslandUnionLogData(HsIslandUnionLog logData) {
        HsIslandProto.HsIslandUnionLogData.Builder builder = HsIslandProto.HsIslandUnionLogData.newBuilder();
        builder.setType(logData.getType());
        builder.setUserId(logData.getUserId());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(logData.getUserId());
        if (userBaseInfo != null) {
            builder.setUserBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        if (logData.getReward() != null) {
            builder.setReward(buildHsIslandRewardData(logData.getReward()));
        }
        builder.setPveMonsterId(logData.getPveMonsterId());
        if (logData.getHelpUserId() > 0) {
            UserBaseInfo helpUserBaseInfo = CrossUserMgr.getUserBaseInfo(logData.getHelpUserId());
            if (helpUserBaseInfo != null) {
                builder.setHelpUserName(helpUserBaseInfo.getNickName());
            }
        }
        builder.setFloor(logData.getFloor());
        builder.setKeyNum(logData.getKeyNum());
        builder.setTime(logData.getTime());
        return builder;
    }

    /**
     * build 用户日志
     * @param logData
     * @return
     */
    public static HsIslandProto.HsIslandUserLogData.Builder buildHsIslandUserLogData(HsIslandUserLog logData) {
        HsIslandProto.HsIslandUserLogData.Builder builder = HsIslandProto.HsIslandUserLogData.newBuilder();
        builder.setType(logData.getType());
        builder.setPveMonsterId(logData.getPveMonsterId());
        if (logData.getReward() != null) {
            builder.setReward(buildHsIslandRewardData(logData.getReward()));
        }
        builder.setValue(logData.getValue());
        builder.setUnionName(logData.getTargetUnionName());
        builder.setBuffId(logData.getBuffId());
        builder.setGiftConfigId(logData.getGiftConfigId());
        builder.setTime(logData.getTime());
        return builder;
    }

    /**
     * build 门客数据
     * @param patronsData
     * @param addRate
     * @return
     */
    public static HsIslandProto.HsIslandPatronsData.Builder buildHsIslandPatronsData(HsIslandPatronsData patronsData, int addRate) {
        HsIslandProto.HsIslandPatronsData.Builder builder = HsIslandProto.HsIslandPatronsData.newBuilder();
        builder.setPatronsId(patronsData.getPatronsId());
        builder.setBasePower(patronsData.getPower());
        long addPower = BigDecimal.valueOf(patronsData.getPower()).multiply(BigDecimal.valueOf(addRate).divide(BigDecimal.valueOf(1000), 8,  RoundingMode.UP)).setScale(0, RoundingMode.CEILING).longValue();
        builder.setPower(patronsData.getPower() + addPower);
        return builder;
    }

    /**
     * 解析门客LIST
     * @param list
     * @return
     */
    public static List<HsIslandPatronsData> parsePatronsList(List<HsIslandProto.HsIslandPatronsData> list) {
        List<HsIslandPatronsData> patronsDataList = new ArrayList<>();
        for (HsIslandProto.HsIslandPatronsData patronData : list) {
            HsIslandPatronsData data = new HsIslandPatronsData();
            data.setPatronsId(patronData.getPatronsId());
            data.setPower(patronData.getBasePower());
            patronsDataList.add(data);
        }
        return patronsDataList;
    }

    /**
     * 战场信息
     *
     * @param battleField 战场
     */
    public static HsIslandProto.HsIslandBattleFieldDataMsg.Builder builderHsIslandBattleFieldData(CrossHsIslandBattleField battleField) {
        HsIslandProto.HsIslandBattleFieldDataMsg.Builder builder = HsIslandProto.HsIslandBattleFieldDataMsg.newBuilder();
        builder.setBattleFieldId(battleField.getBattleFieldId());
        builder.setMonsterData(buildHsIslandMonsterData(battleField.getActivityId(), battleField.getGroupId(), battleField.getMonsterData(), true));
        Map<Long, CrossHsIslandUser> battleUserMap = battleField.getBattleUserMap();
        for (CrossHsIslandUser battleUser : battleUserMap.values()) {
            HsIslandProto.HsIslandBattleFieldUserDataMsg.Builder userTemp = buildHsIslandBattleFieldUserData(battleField, battleUser);
            if (userTemp != null) {
                builder.addUserData(userTemp);
            }
        }
        // 圣柱boss带上伤害信息
        if (battleField.getEnemyType() == HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
            CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(battleField.getActivityId(), battleField.getGroupId());
            // 伤害榜单
            CrossHsIslandBattleFieldDamage unionDamage = activity.getBattleFieldDamage(battleField.getBattleFieldId());
            if (unionDamage != null) {
                // 商会信息
                CrossHsIslandRankListModel unionDamageRankModel = unionDamage.getUnionDamageRankModel();
                if (unionDamageRankModel != null) {
                    List<HsIslandRank> rankList = unionDamageRankModel.getRankList();
                    int rankNum = 0;
                    for (HsIslandRank rank : rankList) {
                        rankNum++;
                        String unionId = rank.getRankId();
                        HsIslandProto.HsIslandBattleFieldUnionDataMsg.Builder unionTemp = HsIslandProto.HsIslandBattleFieldUnionDataMsg.newBuilder();
                        unionTemp.setUnionUid(unionId);
                        UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(battleField.getActivityId(), unionId, battleField.getGroupId());
                        if (groupUnion != null) {
                            unionTemp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(groupUnion.getUnionBaseInfo()));
                        }
                        unionTemp.setRank(rankNum);
                        unionTemp.setDamage(rank.getValue());
                        builder.addUnionData(unionTemp);
                    }
                }
            }
        }
        return builder;
    }


    /**
     * 战场用户信息
     * @param battleField
     * @param user
     * @return
     */
    public static HsIslandProto.HsIslandBattleFieldUserDataMsg.Builder buildHsIslandBattleFieldUserData(CrossHsIslandBattleField battleField, CrossHsIslandUser user) {
        HsIslandProto.HsIslandBattleFieldUserDataMsg.Builder builder = HsIslandProto.HsIslandBattleFieldUserDataMsg.newBuilder();
        long userId = user.getUserData().getUserId();
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
        if (userBaseInfo == null) {
            return null;
        }
        builder.setUserId(userId);
        builder.setPlayerData(PlayerBasePb.parsePlayerSimpleBaseTempMsg(userBaseInfo));
        builder.setPower(user.getPower());
        builder.setPvpEnergy(user.getPvpEnergy());
        // 精力上限
        builder.setPvpEnergyLimit(user.getConfig().getMaxPvpEnergy(user.getUserData().getFloorId()));
        builder.setPvpEnergyLastTime(user.getUserData().getPvpEnergyLastTime());
        builder.setLastAttackMonsterTime(user.getUserData().getLastAttackMonsterTime());
        builder.setLastAttackPlayerTime(user.getUserData().getLastAttackPlayerTime());
        builder.setLastAttackBossTime(user.getUserData().getLastAttackBossTime());
        if (battleField.getEnemyType() == HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
            // 战场内伤害
            long myDamage = 0;
            CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(battleField.getActivityId(), battleField.getGroupId());
            CrossHsIslandBattleFieldDamage battleFieldDamage = activity.getBattleFieldDamage(battleField.getBattleFieldId());
            if (battleFieldDamage != null) {
                HsIslandBattleFieldDamageData damageData = battleFieldDamage.getDamageData(userId);
                if (damageData != null) {
                    myDamage = damageData.getTotalDamage();
                }
            }
            builder.setDamage(myDamage);
        }
        return builder;
    }

    /**
     * build 战斗结果
     * @param battleResult
     * @return
     */
    public static HsIslandProto.HsIslandBattleResultData.Builder buildHsIslandBattleResult(HsIslandBattleResult battleResult) {
        HsIslandProto.HsIslandBattleResultData.Builder builder = HsIslandProto.HsIslandBattleResultData.newBuilder();
        builder.setWinFlag(battleResult.isWinFlag());
        builder.setDamage(battleResult.getDamage());
        return builder;
    }

    /**
     * 构建我的商会信息
     */
    public static HsIslandProto.HsIslandBattleFieldUnionDataMsg.Builder buildHsIslandBattleFieldUnionData(CrossHsIslandBattleField battleField, CrossHsIslandUser user) {
        HsIslandProto.HsIslandBattleFieldUnionDataMsg.Builder unionTemp = HsIslandProto.HsIslandBattleFieldUnionDataMsg.newBuilder();
        String unionId = user.getUserData().getUnionId();
        unionTemp.setUnionUid(unionId);
        UnionActivityGroupUnion groupUnion = Cross2UnionActivityGroupMgr.getUnionData(battleField.getActivityId(), unionId, battleField.getGroupId());
        if (groupUnion != null) {
            unionTemp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(groupUnion.getUnionBaseInfo()));
        }
        // 获取下该战场的榜单信息
        if (battleField.getEnemyType() == HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
            CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(battleField.getActivityId(), battleField.getGroupId());
            CrossHsIslandBattleFieldDamage unionDamage = activity.getBattleFieldDamage(battleField.getBattleFieldId());
            // 我的排名
            unionTemp.setRank(unionDamage == null ? -1 : unionDamage.getMyUnionRank(unionId));
            // 我的伤害
            unionTemp.setDamage(unionDamage == null ? 0 : unionDamage.getMyUnionDamage(unionId));
        } else {
            unionTemp.setRank(-1);
            unionTemp.setDamage(0);
        }
        return unionTemp;
    }
}
